package day01;

/*
 * Author: JacksonCoder
 * FileName: SelectionSort
 * Version: 1.0.0
 * Date: 2024/12/26
 */

public class SelectionSort {
    // 选择排序
    public static void SelectSort(int[] arr) {
        //TODO
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        // 0 ~ N-1
        // 1 ~ N-1
        // 2 ~ N-1
        for (int i = 0; i < N; i++) {
            int minValueIndex = i; // i
            for (int j = i + 1; j < N; j++) {
                minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
            }
            swap(arr, i, minValueIndex);

        }
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = {7, 1, 3, 5, 1, 6, 8, 1, 3, 5, 7, 5, 6};
        int[] arr2 = {6, 3, 5, 9, 7, 0, 2, 8, 4, 1};
        printArray(arr);
        SelectSort(arr);
        printArray(arr);
        System.out.println(" ");
        printArray(arr2);
        SelectSort(arr2);
        printArray(arr2);
    }
}
